package com.hxhk.web.controller.module.cgform.config;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hxhk.pojo.Criteria;
import com.hxhk.pojo.ExtGridReturn;
import com.hxhk.pojo.ExtPager;
import com.hxhk.pojo.module.cgform.config.CgFormHeadEntity;
import com.hxhk.service.module.cgform.config.CgFormHeadServiceI;

/**
 * 智能表单配置
 * @author wangl
 * @date 2014-5-7 下午18:47:37
 */
@Controller
@RequestMapping("/cgFormHeadController")
public class CgFormHeadController {
	private static Logger logger = LoggerFactory.getLogger(CgFormHeadController.class);
	@Autowired
	private CgFormHeadServiceI cgFormHeadService;
	/*@Autowired
	private CgFormFieldServiceI cgFormFieldService;*/
	private String message;

	/**
	 * 自动生成表属性列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "cgFormHeadList")
	public ModelAndView cgFormHead(HttpServletRequest request) {
		return new ModelAndView("module/cgform/config/CgFormHeadList");
	}
	@RequestMapping(params = "goCgFormSynChoice")
	public ModelAndView goCgFormSynChoice(HttpServletRequest request) {
		return new ModelAndView("module/cgform/config/cgformSynChoice");
	}
	
	/**
	 *
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public Object datagrid(ExtPager pager,CgFormHeadEntity cgFormHead,@RequestParam(required = false, defaultValue = "") String tableName,
			HttpServletRequest request, HttpServletResponse response) {
		Criteria criteria = new Criteria();
		// 设置分页信息
		if (pager.getLimit() != null && pager.getStart() != null) {
			criteria.setOracleEnd(pager.getLimit() + pager.getStart());
			criteria.setOracleStart(pager.getStart());
		}
		// 排序信息
		if (StringUtils.isNotBlank(pager.getDir()) && StringUtils.isNotBlank(pager.getSort())) {
			criteria.setOrderByClause(pager.getSort() + " " + pager.getDir());
		}
		if (StringUtils.isNotBlank(tableName)) {
			criteria.put("tableName", tableName);
		}
		List<CgFormHeadEntity> list = this.cgFormHeadService.selectByExample(criteria);
		int total = this.cgFormHeadService.countByExample(criteria);
		return new ExtGridReturn(total, list);
	}
	
	/**
	 * 保存用户信息
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Object save(CgFormHeadEntity cgFormHead,HttpServletRequest request) {
		CgFormHeadEntity oldTable = cgFormHeadService.selectByPrimaryKey(cgFormHead.getId());
		return null;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
